#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"

// 创建一个简单的JSON对象并返回
char* create_simple_json() {
    cJSON *root = cJSON_CreateObject();
    if (root == NULL) {
        return NULL;
    }
    
    // 添加键值对
    cJSON_AddStringToObject(root, "name", "JSONDemo");
    cJSON_AddNumberToObject(root, "version", 1.0);
    cJSON_AddBoolToObject(root, "enabled", cJSON_True);
    
    // 创建一个数组
    cJSON *array = cJSON_CreateArray();
    if (array == NULL) {
        cJSON_Delete(root);
        return NULL;
    }
    
    cJSON_AddItemToArray(array, cJSON_CreateString("feature1"));
    cJSON_AddItemToArray(array, cJSON_CreateString("feature2"));
    cJSON_AddItemToArray(array, cJSON_CreateString("feature3"));
    
    // 将数组添加到根对象
    cJSON_AddItemToObject(root, "features", array);
    
    // 转换为字符串
    char *json_str = cJSON_Print(root);
    
    // 释放JSON对象
    cJSON_Delete(root);
    
    return json_str;
}

// 解析JSON字符串
int parse_json_string(const char *json_str) {
    // 解析JSON
    cJSON *root = cJSON_Parse(json_str);
    if (root == NULL) {
        const char *error_ptr = cJSON_GetErrorPtr();
        if (error_ptr != NULL) {
            fprintf(stderr, "Error before: %s\n", error_ptr);
        }
        return -1;
    }
    
    // 提取数据
    cJSON *name = cJSON_GetObjectItemCaseSensitive(root, "name");
    if (cJSON_IsString(name) && (name->valuestring != NULL)) {
        printf("Name: %s\n", name->valuestring);
    }
    
    cJSON *version = cJSON_GetObjectItemCaseSensitive(root, "version");
    if (cJSON_IsNumber(version)) {
        printf("Version: %.1f\n", version->valuedouble);
    }
    
    cJSON *enabled = cJSON_GetObjectItemCaseSensitive(root, "enabled");
    if (cJSON_IsBool(enabled)) {
        printf("Enabled: %s\n", enabled->valueint ? "true" : "false");
    }
    
    // 解析数组
    cJSON *features = cJSON_GetObjectItemCaseSensitive(root, "features");
    if (cJSON_IsArray(features)) {
        int array_size = cJSON_GetArraySize(features);
        printf("Features count: %d\n", array_size);
        
        for (int i = 0; i < array_size; i++) {
            cJSON *item = cJSON_GetArrayItem(features, i);
            if (cJSON_IsString(item) && (item->valuestring != NULL)) {
                printf("Feature %d: %s\n", i + 1, item->valuestring);
            }
        }
    }
    
    // 释放JSON对象
    cJSON_Delete(root);
    
    return 0;
}

// 嵌套JSON对象示例
char* create_nested_json() {
    cJSON *root = cJSON_CreateObject();
    if (root == NULL) {
        return NULL;
    }
    
    cJSON_AddStringToObject(root, "type", "example");
    
    // 创建嵌套对象
    cJSON *details = cJSON_CreateObject();
    if (details == NULL) {
        cJSON_Delete(root);
        return NULL;
    }
    
    cJSON_AddNumberToObject(details, "id", 123);
    cJSON_AddStringToObject(details, "description", "JSON nested object example");
    
    // 将嵌套对象添加到根对象
    cJSON_AddItemToObject(root, "details", details);
    
    // 转换为字符串
    char *json_str = cJSON_Print(root);
    
    // 释放JSON对象
    cJSON_Delete(root);
    
    return json_str;
}

// 修改JSON对象中的值
char* modify_json(const char *original_json) {
    cJSON *root = cJSON_Parse(original_json);
    if (root == NULL) {
        return NULL;
    }
    
    // 修改已存在的值
    cJSON *version = cJSON_GetObjectItemCaseSensitive(root, "version");
    if (cJSON_IsNumber(version)) {
        cJSON_ReplaceItemInObject(root, "version", cJSON_CreateNumber(1.1));
    }
    
    // 添加新的键值对
    cJSON_AddStringToObject(root, "author", "JSONDemo Developer");
    
    // 转换为字符串
    char *modified_json = cJSON_Print(root);
    
    // 释放JSON对象
    cJSON_Delete(root);
    
    return modified_json;
}

// 主入口函数
int make(void) {
    printf("===== C JSON Parser Demo =====\n\n");
    
    // 创建并解析简单JSON对象
    printf("1. Create and parse simple JSON object:\n");
    char *json_str = create_simple_json();
    if (json_str != NULL) {
        printf("Created JSON:\n%s\n\n", json_str);
        
        printf("Parsing result:\n");
        if (parse_json_string(json_str) != 0) {
            printf("JSON parsing failed\n");
        }
        
        // 修改JSON
        printf("\n2. Modify JSON object:\n");
        char *modified_json = modify_json(json_str);
        if (modified_json != NULL) {
            printf("Modified JSON:\n%s\n\n", modified_json);
            free(modified_json);
        }
        
        free(json_str);
    }
    
    // 创建嵌套JSON对象
    printf("3. Create nested JSON object:\n");
    char *nested_json = create_nested_json();
    if (nested_json != NULL) {
        printf("Nested JSON:\n%s\n\n", nested_json);
        free(nested_json);
    }
    
    // 错误处理示例
    printf("4. Error handling example:\n");
    const char *invalid_json = "{\"name\": }";
    printf("Invalid JSON: %s\n", invalid_json);
    if (parse_json_string(invalid_json) != 0) {
        printf("Expected parsing failure\n");
    }
    
    printf("\n===== Demo completed =====\n");
    
    return 0;
}

// 程序入口点
int main(int argc, char *argv[]) {
    (void)argc; // 避免未使用参数的警告
    (void)argv; // 避免未使用参数的警告
    return make();
}